package com.dcivision.framework.taglib.channel;

import java.util.Iterator;
import java.util.List;

import javax.servlet.jsp.PageContext;

import com.dcivision.calendar.bean.CalendarRecord;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.taglib.utility.SourceManage;
import com.dcivision.user.UserHomePreferenceConstant;

public abstract class AjaxStandardMeetingFormatter extends AbstractAjaxChannelFormatter {
//--should init---
  protected AjaxTagFactory  ajaxFactory = null;

  protected CalendarRecord calRecord = null;

  public AjaxStandardMeetingFormatter(){
    //empty
  }
  public AjaxStandardMeetingFormatter(PageContext pageContext,int pageSize,List channelList){
    this.pageContext = pageContext;
    this.channelList = channelList;
    this.pageSize = pageSize;
    init();
  }

  public void init(){
    super.init();
    ajaxFactory = (AjaxTagFactory)pageContext.getRequest().getAttribute(AjaxConstant.AJAXTAGFACTORY);
    this.columnCount = 3;
    this.pattern="common.channel.meeting_event";
  }

  public String[] getContent() throws ApplicationException {
    
    StringBuffer bstr = new StringBuffer();
    for(Iterator it=channelList.iterator();it.hasNext();){
      calRecord = (CalendarRecord)it.next();
      bstr.append("<tr>");
      //bstr.append(getIcon());
      bstr.append(getPriority());
      bstr.append(getTitle());
      bstr.append(getStartDate());
      bstr.append("</tr>");
    }
    //bstr.append(this.getPageInfoContent(columnCount));
    //return bstr.toString();
    return parsePageInfo(bstr);
  }

  protected abstract String getIcon() throws ApplicationException;
  protected abstract String getPriority() throws ApplicationException;
  protected abstract String getTitle() throws ApplicationException;
  protected abstract String getStartDate() throws ApplicationException;

  protected String getIcon(StringBuffer bstr){
    if( CalendarRecord.EVENT_TYPE_CALENDAR_MEETING.equals(calRecord.getEventType()) ){
      if(sessionContainer.getUserRecordID().equals(calRecord.getCreatorID())){
        bstr.append("<img src=\""+SourceManage.getImgSrc(pageContext, "IconObjectMeeting.gif", true, false)+"\" border=0 alt='"+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "calendar.label.event_calendar_meeting")+"' width='16' height='16'/>");
      }else{
        bstr.append("<img src=\""+SourceManage.getImgSrc(pageContext, "IconStatusInviteMeeting.gif", true, false)+"\" alt='"+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "calendar.label.event_calendar_meeting")+"' width='16' height='16'/>");
      }
    }else{
      if(sessionContainer.getUserRecordID().equals(calRecord.getCreatorID())){
        bstr.append("<img src=\""+SourceManage.getImgSrc(pageContext, "IconObjectEvent.gif", true, false)+"\" alt='"+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "calendar.label.event_calendar_event")+"' width='16' height='16'/>");
      }else{
        bstr.append("<img src=\""+SourceManage.getImgSrc(pageContext, "IconStatusInviteEvent.gif", true, false)+"\" alt='"+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "calendar.label.event_calendar_meeting")+"' width='16' height='16'/>");
      }
    }
    return bstr.toString();
  }

  protected String getPriority(StringBuffer bstr){
    String priority = "IconPriorityHomeLow.gif";
    String altMsg = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "home.label.icon_priority_low");
    if (GlobalConstant.PRIORITY_HIGH.equals(calRecord.getPriority())) {
      priority = "IconPriorityHomeHigh.gif";
      altMsg = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "home.label.icon_priority_high");
    } else if (GlobalConstant.PRIORITY_MEDIUM.equals(calRecord.getPriority())) {
      priority = "IconPriorityHomeMedium.gif";
      altMsg = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "home.label.icon_priority_medium");
    }
    bstr.append("<img src='"+SourceManage.getImgSrc(pageContext, priority, true, false)+"' alt='"+altMsg+"' width='16' height='16'/>");
    return bstr.toString();
  }

  protected String getTitle(StringBuffer bstr) throws ApplicationException{
   // bstr.append("<a href=\"#\" onclick=\"ViewMeeting('"+(String)pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY)+"/calendar/MaintCalendar.do?SHOW_HEADER_FOOTER=N&navMode="+GlobalConstant.NAV_MODE_EDIT+"&ID="+calRecord.getID()+"&isHome=true')\" title='"+calRecord.getTitle()+"'>");
    //bstr.append("<a href=\""+(String)pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY)+"/calendar/MaintCalendar.do?navMode="+GlobalConstant.NAV_MODE_VIEW+"&ID="+calRecord.getID()+"&isHome=true\" title='"+calRecord.getTitle()+"' class=\"normallink\">");
    bstr.append("<a href=\""+(String)pageContext.getServletContext().getAttribute(GlobalConstant.CONTEXT_PATH_KEY)+"/calendar/MaintCalendar.do?navMode="+GlobalConstant.NAV_MODE_VIEW+"&ID="+calRecord.getID()+"&isHome=true\" title='"+TextUtility.replaceString(calRecord.getTitle(),"'","&#39")+"' class=\"normallink\">");//Update By Jim Zhou    2006/12/14      EIP-1213
    String showValue = "";
    AjaxSummarizeContentTag tag = (AjaxSummarizeContentTag)ajaxFactory.getAjaxTag(pageContext, AjaxConstant.summarizeContentTag);
    tag.setKey(calRecord.getTitle());
    if(sessionContainer.getUserRecordID().equals(calRecord.getCreatorID())){
      tag.setLimitLength(UserHomePreferenceConstant.DEFAULT_MAX_MEETING_TITLE_LENGTH+20);
      showValue = tag.getAjaxContent();
    } else {
      String calendarCreatorName = UserInfoFactory.getUserFullName(calRecord.getCreatorID());
      tag.setLimitLength(UserHomePreferenceConstant.DEFAULT_MAX_MEETING_TITLE_LENGTH);
      showValue =tag.getAjaxContent()+" ["+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(),"home.message.meeting_host")+" "+calendarCreatorName+"]";
    }
    ajaxFactory.releaseTag(AjaxConstant.summarizeContentTag, tag);
    bstr.append(showValue);
    if ("Y".equals(calRecord.getIsWholeDay())) {
      bstr.append("["+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "calendar.label.is_whole_day_event")+"]");
    }
    bstr.append("</a>");
    return bstr.toString();
  }

  protected String getStartDate(StringBuffer bstr){
    /*if (!"Y".equals(calRecord.getIsWholeDay())) {
    bstr.append(TextUtility.formatDate(calRecord.getDatetime(),SystemParameterFactory.getSystemParameter(SystemParameterConstant.DATETIME_FORMAT)));
    }else{
    bstr.append(TextUtility.formatTimestamp(calRecord.getDatetime()).substring(0,10));
    }*/
    //Logic to get the default date time format from system    
    java.text.SimpleDateFormat formatter = TextUtility.getDateTimeFormat();
    bstr.append(formatter.format(calRecord.getDatetime()));
    return bstr.toString();
  }

}
